<?php

namespace app\admin\controller;
use think\Db;

class Assist extends Common{

	public function index(){
		return $this->fetch();
	}
	/*
	数据接口
	*/
	public function dataList(){
		$limit = request()->param('limit')?:20;
		$username   = request()->param('username');
		$status   = request()->param('status');
		$start_time = request()->param('start_time');
		$end_time   = request()->param('end_time');
		$uid   = request()->param('uid');
		$where      = '1=1';
		if ($username != '') {
            $where .= ' and u.username like \'%' . $username . '%\'';
        }
        if($status != '') {
            $where .= ' and a.status='.$status;
        }
        if($start_time != '') {
            $where .= ' and a.date>=\''.$start_time.'\'';
        }
		if($end_time != '') {
            $where .= ' and a.date<=\''.$end_time.'\'';
        }
         if($uid != '') {
            $where .= ' and a.uid='.$uid;
        }
		$data  = Db::table('tp_assist')
				->alias('a')
				->join('tp_user u','u.id = a.uid','LEFT')
				->where($where)
				->order('id desc')
				->field('a.*,u.username')
				->paginate($limit);
		$data = $data->toArray();
		if(empty($data['data'])){
			$page = 1;
			$data['data']  = Db::table('tp_assist')
				->alias('a')
				->join('tp_user u','u.id = a.uid','LEFT')
				->where($where)
				->order('id desc')
				->field('a.*,u.username')
				->page(1)
				->limit($limit)
				->select();		
		}
		return $this->tableData($data);
	}
	/*
	客服回复
	 */
	public function reply(){
		if (request()->method() == 'POST') {
			$id  = request()->param('id');
			$answer = request()->param('answer');
			$res  = Db::table('tp_assist')->where('id',$id)->update(['answer'=>$answer,'status'=>1,'rep_date'=>date('Y-m-d H:i:s',time())]);
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		}
	}
}